//
//  HXLeftDropdownMenu.m
//  KD
//
//  Created by 梅洋 on 16/8/3.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXLeftDropdownMenu.h"

@interface HXLeftDropdownMenu()
/**
 *  将来用来显示具体内容的容器
 */
@property (nonatomic, weak) UIImageView *containerView;
@end


@implementation HXLeftDropdownMenu

+ (instancetype)menu
{
    return [[self alloc] init];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 清除颜色
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setContent:(UIView *)content
{
    _content = content;
    
    // 调整内容的位置
    content.x = 8;
    content.y = 10;
    
    // 调整内容的宽度
    //    content.width = self.containerView.width - 2 * content.x;
    
    // 设置灰色的高度
    self.containerView.height = content.size.height+14;
    // 设置灰色的宽度
    self.containerView.width = content.size.width + 17;
    
    // 添加内容到灰色图片中
    [self.containerView addSubview:content];
    
}

- (void)setContentController:(UIViewController *)contentController
{
    _contentController = contentController;
    
    self.content = contentController.view;
}

/**
 *  显示
 */
- (void)showFrom:(UIView *)from
{
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window addSubview:self];
    self.frame = window.bounds;
    CGRect newFrame = [from convertRect:from.bounds toView:window];
    self.containerView.centerX = CGRectGetMaxX(newFrame)+42;
    self.containerView.y = CGRectGetMaxY(newFrame)-6;
}

-(void)showView{

    self.hidden = NO;
}

/**
 *  销毁
 */
- (void)dismiss
{
//    self.hidden = YES;
    [self removeFromSuperview];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}


#pragma mark -  containerView懒加载
- (UIImageView *)containerView
{
    if (!_containerView) {
        UIImage *image = [UIImage imageNamed:@"leftMenuView"];
        CGFloat w = image.size.width;
        CGFloat h = image.size.height;
        UIImage *newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(h*0.5-1, w*0.5-1,h*0.5-1, w*0.5-1)];
        // 添加一个灰色图片控件
        UIImageView *containerView = [[UIImageView alloc] initWithImage:newImage];
        containerView.userInteractionEnabled = YES;
        [self addSubview:containerView];
        self.containerView = containerView;
    }
    return _containerView;
}

@end
